﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ChurchDiary.DAL.Contract;
using ChurchDiary.DAL.Methods;
using ChurchDiary.TableEntity;
using System.Xml;
using ChurchDiary.Common;
using System.Net.Mail;
using System.Net;
using System.Web;

namespace ChurchDiary.BLL
{
    public class BLL_Common
    {
        public List<EntityCountry> GetCountryList()
        {
            IDALCommon CList = new DAL_Common();
            return CList.GetCountry();
        }
        public List<EntityStates> GetStateList(Guid CountryId)
        {
            IDALCommon CList = new DAL_Common();
            return CList.GetState(CountryId);
        }
        #region Email
        public EntityEmail GetEmailsByUserId(Guid userId, String emailType)
        {
            IDALCommon CList = new DAL_Common();
            return CList.GetEmailsByUserId(userId, emailType);
        }
        public string SendEmail(EntityEmail Email)
        {
            string eResult = "ERROR";
            try
            {
                MailMessage MailMessage = new MailMessage();
                MailMessage.To.Add(new MailAddress(Email.EmailToAddress));

                if (!string.IsNullOrEmpty(Email.EmailBcc))
                {
                    MailMessage.Bcc.Add(Email.EmailBcc);
                }
                if (!string.IsNullOrEmpty(Email.EmailCC))
                {
                    MailMessage.Bcc.Add(Email.EmailCC);
                }
                MailMessage.From = new MailAddress("noreplay@fgpc.in");
                MailMessage.Subject = Email.EmailSubject;
                MailMessage.IsBodyHtml = true;

                string Content = GetEmailTemplate(Email);
                MailMessage.Body = Content;

                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Host = "mail.fgpc.in"; //-- Donot change.
                smtpClient.Port = 25; //--- Donot change
                smtpClient.EnableSsl = false;//--- Donot change
                smtpClient.UseDefaultCredentials = true;
                smtpClient.Credentials = new System.Net.NetworkCredential("noreplay@fgpc.in", "Ju$t!n$a@m123");
                smtpClient.Send(MailMessage);

                Email.EmaiStatus = 1;
                string Result = new BLL_Common().UpdateEmail(Email);
                eResult = "SUS";
            }
            catch (Exception ex)
            {
                Email.EmailSendOn = DateTime.Now;
                Email.EmaiStatus = 2;
                string Result = new BLL_Common().UpdateEmail(Email);
                eResult = "ERROR";
            }
            return eResult;
        }
        public static string GetEmailTemplate(EntityEmail email)
        {
            string Template = "";
            switch (email.EmailType)
            {
                case "LOGIN":
                    Template = GetLoginTemplate(email);
                    break;
                default:
                    Template = "No Template found";
                    break;
            }
            return Template;
        }
        public static string GetLoginTemplate(EntityEmail email)
        {
            string Template = "";
            Template = email.EmailContent.ToString();
            EntityUser User = new BLL_User().GetUserInfo(email.EmailSourceId);
            Template = Template.Replace("[USERNAME]", User.UserName);
            Template = Template.Replace("[PASSWORD]", User.Password);
            Template = Template.Replace("[NAME]", User.Name);
            EntityOrganizationInfo OrganizationInfo = new BLL_Organization().GetOrganizationInfo();
            Template = Template.Replace("[OrganizationName]", OrganizationInfo.NameOfOrganization);
            Template = Template.Replace("[OrganizationLogo]", OrganizationInfo.OrganizationLogo);
            Template = Template.Replace("[WEBSITE]", OrganizationInfo.OrganizationWebsite);
            EntitySoftwareInfo SoftwareInfo = new BLL_Organization().GetSoftwareInfo();
            Template = Template.Replace("[SoftwareLogo]", SoftwareInfo.SoftwareLogo);
            Template = Template.Replace("[SoftwareName]", SoftwareInfo.SoftwareName);
            Template = Template.Replace("[SuggestionLink]", SoftwareInfo.SoftwareSupportLink);
            //
            Template = Template.Replace("[LINK]", CommonFunction.GetApplicationLink());
            Template = Template.Replace("[Email Subject]", email.EmailSubject);
            Template = Template.Replace("[USERID]", email.EmailSourceId.ToString());
            return Template;
        }
        public List<EntityEmail> GetEmails()
        {
            IDALCommon CList = new DAL_Common();
            return CList.GetEmails();
        }
        public List<EntityEmail> GetPendingEmails()
        {
            IDALCommon CList = new DAL_Common();
            return CList.GetPendingEmails();
        }
        public string InsertEmail(EntityEmail Email)
        {
            IDALCommon CList = new DAL_Common();
            return CList.InsertEmail(Email);
        }
        public string UpdateEmail(EntityEmail Email)
        {
            IDALCommon CList = new DAL_Common();
            return CList.UpdateEmail(Email);
        }
        public  string ActivateUserAccount(Guid userId)
        {
            IDALCommon CList = new DAL_Common();
            return CList.ActivateUserAccount(userId);
        }
        #endregion
    }
}
